package czx.wt.web.security;

import czx.wt.util.encrypt.AesEncryptUtil;
import czx.wt.util.request.RequestContextUtil;
import czx.wt.util.response.CommResponseEnum;
import czx.wt.util.response.Resp;
import czx.wt.util.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 自定义密码加密
 * @Author: ChenZhiXiang
 * @CreateDate: 2018/12/20 0020 14:34
 * @Version: 1.0
 */
public class MyPasswordEncoder implements PasswordEncoder {

    private static final Logger logger = LoggerFactory.getLogger(MyPasswordEncoder.class);

    @Override
    public String encode(CharSequence charSequence) {
        logger.info("前端传过来的密码"+charSequence.toString());
        try {
            String pwd = AesEncryptUtil.encrypt(charSequence.toString());
            logger.info("密码加密"+pwd);
           return pwd;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        try {
            if (AesEncryptUtil.encrypt(charSequence.toString()).equals(s)){
                logger.info("密码对比"+AesEncryptUtil.encrypt(charSequence.toString())+"***"+s);
                logger.info("登录成功");
                return true;
            } else {
                Resp.outResp(RequestContextUtil.getResponse(),ResponseData.failureResponse(CommResponseEnum.USER2));
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
